package com.teach.servlet;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.bcel.internal.generic.LLOAD;
import com.teach.entity.user;
import com.teach.service.UserService;
import com.teach.service.impl.UserServiceImpl;


public class UserUpdateServlet extends HttpServlet{
   private UserService userService=new UserServiceImpl();
   user u = new user();
   SimpleDateFormat format=new SimpleDateFormat("yyyy-mm-dd");
	@Override
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		Integer userId = Integer.valueOf(request.getParameter("userId"));
		u.setId(userId);
		u.setName(request.getParameter("userName"));
		u.setPassword(request.getParameter("userPassword"));
		u.setNickname(request.getParameter("userNickname"));
		u.setAccount_number(request.getParameter("account_number"));
		u.setBirthday(Date.valueOf(request.getParameter("userBrithdate")));
		u.setUser_address(request.getParameter("user_address"));
		u.setPhone(request.getParameter("userPhone"));
		
		Date date=Date.valueOf(request.getParameter("creat_time"));
		u.setCreate_time(date);
		
		
		try{//需要非String类型的时候，需要进行类型的转换
			String userSex = request.getParameter("userSex");
			String sex=userSex.equals("true")?"男":"女";
			
			u.setSex(sex);
		}catch(Exception ex){}
		
		
		try{
			userService.updateById(u);
			request.setAttribute("msg", "修改用户成功");
			request.setAttribute("href","user.let");
		}catch(Exception ex){
			ex.printStackTrace();//记调试日志
			request.setAttribute("error", "修改用户失败:"+ex.getMessage());
		}
		//跳转到视图
		request.getRequestDispatcher("/admin/msg.jsp").forward(request, response);
		}
}
